A magic number is a value with a special meaning. It will never be changed within the program, it is instead a constant which is used within it. I do not like the idea of "magic numbers" in programs, what I would like to do is replace each number with something a bit more meaningful.

#define PI 3.141592654

Whenever the pre-processor sees PI it sends the compiler 3.141592654. This means that you can do things like:

circ = rad * 2 * PI ;

The item which follows the #define directive should be a sequence of characters. You then have a space, followed by another sequence of characters. Neither of the two sequences are allowed to contains spaces, this would get the pre-processor confused. Anywhere you use a magic number you should use a definition of this form, for example:

#define MAX_SIZE 5

This makes your programs much easier to read, and also much easier to change. There is a C convention that you always give the symbol you are defining in CAPITAL LETTERS. This is so that when you read your program you can tell which things have been defined.

On the right you can see #define used to create the structures for our bank. We have set the sizes using #defined items. The compiler sees the values after the pre-processor has substituted them.

 

main.c.

#define MAX_CUST 50
#define NAME_LENGTH 30
#define ADDR_LENGTH 60

struct customer
{
   char name [NAME_LENGTH] ;
   char address [ADDR_LENGTH];
   int account ;
   int balance ;
   int overdraft ;
} ;

/*  declare a single    */
/*  customer structure  */
struct customer OnlyOne ;

/*  declare an array    */
/*  of customers        */
struct customer EveryOne [MAX_CUST];

COMPILER

struct customer
{
   char name [30] ;
   char address [60] ;
   int account ;
   int balance ;
   int overdraft ;
} ;

/*  declare a single    */
/*  customer structure  */
struct customer OnlyOne ;

/*  declare an array    */
/*  of customers        */
struct customer EveryOne [50];